﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FTPLib;
using System.Net;
using System.IO;
using System.Xml;
using System.Text.RegularExpressions;

namespace MyFTPClient
{
    public partial class Form1 : Form
    {
        FTPCore __core=new FTPCore();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            

            txtsrvip.Text = __core.FTPSERVERIP;
            txtusername.Text = __core.FTPUSERID;
            txtpassword.Text = __core.FTPPASSWORD;
            
            
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
           
            __core.FTPSERVERIP = txtsrvip.Text.Trim();
            __core.FTPUSERID = txtusername.Text.Trim();
            __core.FTPPASSWORD = txtpassword.Text.Trim();
           
            if (__core.TestConnection(__core.FTPSERVERIP, __core.FTPUSERID, __core.FTPPASSWORD) == true)
            {
                txtsrvip.Enabled = false;
                txtusername.Enabled = false;
                txtpassword.Enabled = false;
                btnConnect.Enabled = false;
                btnDown.Enabled = true;
                //btnUpload.Enabled = true;
                btnRead.Enabled = true;
            }
            else
            {
                txtsrvip.Enabled = true;
                txtusername.Enabled = true;
                txtpassword.Enabled = true;
                btnConnect.Enabled = true;
                btnDown.Enabled = false;
                btnUpload.Enabled = false;
                btnRead.Enabled = false;
                
            }
        }

        private void btnRead_Click(object sender, EventArgs e)
        {
            //string fName = @"C:\Users\DevArea\Desktop\nice book\test.txt";
            
            //__core = new FTPCore();
            //__core.ReadFile("test.txt");
            //OpenFileDialog opFilDlg = new OpenFileDialog();
            FolderBrowserDialog fldDlg = new FolderBrowserDialog();
            if (fldDlg.ShowDialog() == DialogResult.OK)
            {
                __core.ReadFile(fldDlg.SelectedPath, "test.txt");
                //__core.UploadTo("TEST_XML.xml");
            }
            /*try
            {
                FtpWebRequest tmp = (FtpWebRequest)WebRequest.Create(new Uri("ftp://" + __core.FTPSERVERIP + "/"));
                tmp.Method = WebRequestMethods.Ftp.UploadFile;
                tmp.Credentials = new NetworkCredential(__core.FTPUSERID, __core.FTPPASSWORD);

                
                StreamReader readerr = new StreamReader("TEST_XML.xml");
                byte[] Content = Encoding.UTF8.GetBytes(readerr.ReadToEnd());
                readerr.Close();

                tmp.ContentLength = Content.Length;

                Stream ReqStream = tmp.GetRequestStream();
                ReqStream.Write(Content, 0, Content.Length);
                ReqStream.Close();

                FtpWebResponse response = (FtpWebResponse)tmp.GetResponse();
                response.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Upload Error");
            }*/
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory = fldDlg.SelectedPath;
            openFileDialog1.Filter = "xml files (*.xml)|*.xml";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                __core.UploadTo(openFileDialog1.FileName);
            }
            

        }

        private void btnUpload_Click(object sender, EventArgs e)
        {
            //__core = new FTPCore();
            OpenFileDialog opFilDlg = new OpenFileDialog();
            if (opFilDlg.ShowDialog() == DialogResult.OK)
            {
                __core.UploadTo(opFilDlg.FileName);
            }
            
            
        }

        private void btnDown_Click(object sender, EventArgs e)
        {
            //__core = new FTPCore();
            FolderBrowserDialog fldDlg = new FolderBrowserDialog();
            if (txtUpload.Text.Trim().Length > 0)
            {
                if (fldDlg.ShowDialog() == DialogResult.OK)
                {
                    __core.Download(fldDlg.SelectedPath, txtUpload.Text.Trim());
                }
            }
            else
            {
                MessageBox.Show("Please enter the File name to download");
            }

           
            Regex linePattern = new Regex(@"^(?<FileNo>\d{5})(?<CreationDate>\d{8})(?<DeliveryDate>\d{8})");
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            using (XmlWriter writer = XmlWriter.Create(@"C:\Users\DevArea\Desktop\T324\XMLTEST.xml", settings))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement("Root");
                using (StreamReader reader = new StreamReader(@"C:\Users\DevArea\Desktop\T324\test.txt"))
                {
                    while (!reader.EndOfStream)
                    {
                        string line = reader.ReadLine();
                        Match match = linePattern.Match(line);
                        if (match.Success)
                        {
                            writer.WriteStartElement("Line");
                            writer.WriteElementString("FileNo", match.Groups["FileNo"].Value);
                            writer.WriteElementString("CreationDate", match.Groups["CreationDate"].Value);
                            writer.WriteElementString("DeliveryDate", match.Groups["DeliveryDate"].Value);
                            writer.WriteEndElement();
                        }
                    }
                }
                writer.WriteEndElement();
                writer.WriteEndDocument();
            } 

        }
    }
}
